import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const Button 	= $Lazy.load(()=>import('@antd/button'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
const StatusData = [
	{ name: '启用', value: '1' },
	{ name: '禁用', value: '0' },
	{ name: '删除', value: '-1' }
]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:{},
		value:null,
		selectedKeys:[],
		// 搜索表单列表
		formData : [
			{ label: '',	disabled:false,  name:'lab_number',	full:true, p:'请选择实验号模板', type:'select', idStr:'lab_number', afterSuffix:()=>(
				<>
					<Button label='查询' className='mlr15' disabled={ !this.state?.value || this.state.submitLoading } loading={this.state.pullLoading} onClick={this.onSearch} />
					<Button label='重置' disabled={ !this.state?.uuid || this.state.pullLoading } onClick={()=> this.formRef.submit()}/>
				</>
			), onValue:({ value })=>{
				this.setState({ value }, ()=> this.onSearch() )
			} },
			{ label: '模板名称',		name: 'lab_name', 		full:true },
			{ label: '模板编号',		name: 'lab_number', 	full:true },
			{ label: '当前实验号',	name: 'last_code', 		full:true },
			{ label: '结束时间',		name: 'cycle_end', 		half:true },
			{ label: '状态',		name: 'enabled', 		half:true, type:'select', data:StatusData },
			{ label: '描述',		name: 'description', 	full:true, type:'textarea' },
		],
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
	}
	
	//获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			// 实验号模板
			$fn.setRow( formData, 'lab_number', await f.getCache({ api:'num_template' } ) )
			// 
			this.setState({ formData })
		})
	}
	
	// 查询
	onSearch = () => {
		$http.pull(this,'lis-lab-tag/getLabTagInfo', { param:{ lab_number: this.state.value }, loading:true,onError:()=>{
			this.formRef.reset()
			this.setState({ uuid : null })
		} }).then( data =>{
			this.formRef.setValue(data)
			this.setState(()=>{
				return { uuid: $fn.hasObject(data) ? data.uuid : null }
			})
		})
	}

	render(){
		return (
			<Box title='重置实验号' className='ex mt5' small scroll scrollClassName='p10' flex>
				{ 
					SubmitForm.call(this, { msg:'重置', labelWidth: 's6', disabled:true, api:'lis-lab-tag/resetLabTag', 
						query: () => ({ uuid: this.state.uuid })
					})
				}
			</Box>
		)
	}
}

export default Index